﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;

namespace HRWEB.HtmlHelpers
{
    public static class ModelExtensions
    {
        public static string FindHeadshipType(this THeadShip headship)
        {
            string typeString = headship.FHeadShipCode.Substring(0, 2);
            hrdbEntities ent = new hrdbEntities();
            var scale = from t in ent.TDictHeadShips where t.FHeadShipCode == typeString select t.FName;
            if (scale.Any())
            {
                return scale.Single();
            }
            else
            {
                return "未知";
            }
        }
        public static string IsSubmitted(this TEmployee emp)
        {
            return emp.TInfoSummaries.Any() ? (emp.TInfoSummaries.OrderByDescending(p => p.SerialNO).FirstOrDefault().FullyFilled ? "是" : "否") : "否";
        }
        public static string Feedback(this TEmployee emp)
        {
            return emp.TInfoSummaries.Any() ? emp.TInfoSummaries.OrderByDescending(p => p.SerialNO).FirstOrDefault().Feedback : "";
        }
        public static string GetKQDeptCode(this TEmployee emp)
        {
            return emp.TDepts.OrderBy(x => x.FValidDate).Last().FKQDeptCode;
        }
        public static string GetDeptName(this TEmployee emp)
        {
            string deptCode = emp.TDepts.OrderByDescending(x => x.FValidDate).First().FDeptCode;
            using (hrdbEntities ent = new hrdbEntities())
            {
                return ent.TDictDepts.Where(p => p.FDeptCode == deptCode).Select(p => p.FName).First();
            }
        }
        public static string GetBelongOrgan(this TEmployee emp)
        {
           
                if (emp.FBelongOrganCode == null)
                {
                    return "未知";
                }
                else if (emp.FBelongOrganCode == "09")
                {
                    return "外聘";
                }
                else if (emp.FBelongOrganCode == "10")
                {
                    return "借调";
                }
                string code = emp.FBelongOrganCode.Substring(0, 4);
                switch (code)
                {
                    case "0101":
                        return "省编";
                    case "0102":
                        return "合同";
                    case "0201":
                        return "黄岛编制";
                    case "0202":
                        return "黄岛合同";
                    case "0301":
                        return "医学院";
                    case "0302":
                        return "心血管医院";
                    case "0401":
                        return "善达公司";
                    case "0402":
                        return "善达合同";
                    default:
                        return "未知";
                }
            
        }
        public static int GetAge(this TEmployee emp)
        {
            if (emp.FBirthday.HasValue) return DateTime.Now.Year - emp.FBirthday.Value.Year; else return 0;
        }
        public static int GetWorkedYears(this TEmployee emp)
        {
            return emp.FFirstWorkDate.HasValue ? DateTime.Now.Year - emp.FFirstWorkDate.Value.Year : 0;
        }
        public static int GetIntoHisYears(this TEmployee emp)
        {
            return emp.FIntoHisDate.HasValue ? DateTime.Now.Year - emp.FIntoHisDate.Value.Year : 0;
        }
        public static void Clone(this TInfoSummary s1, TInfoSummary s2)
        {
            s1.FEmployeeID = s2.FEmployeeID;
            s1.Feedback = s2.Feedback;
            s1.FullyFilled = s2.FullyFilled;
        }
        public static decimal GetInflunceFactor(this Thesis th)
        {
            return th.Magazine.MagazineInflunceFactors.Where(p => p.Year == th.Date.Year).Select(p => p.Factor).SingleOrDefault();
        }
        public static string GetMagazineGrade(this Thesis th)
        {
            return th.Magazine.MagazineInflunceFactors.Where(p => p.Year == th.Date.Year).Select(p => p.DictMagazineGrade.FName).SingleOrDefault();
        }
        //这些方法并未实际用在下载文件的链接上 
        public static string GetCertFilePath(this TEduStory s)
        {
            return Path.Combine(HttpContext.Current.Server.MapPath("~/CertFiles/EduLevel"), s.Filename);
        }
        public static string GetCertFilePath(this TDegree s)
        {
            return Path.Combine(HttpContext.Current.Server.MapPath("~/CertFiles/Degree"), s.Filename);
        }
        public static string GetCertFilePath(this TTitle s)
        {
            return Path.Combine(HttpContext.Current.Server.MapPath("~/CertFiles/Title"), s.Filename);
        }
        public static bool Validate(this THouseHold hh)
        {
            bool t = hh.FBirthday != null && !string.IsNullOrEmpty(hh.FCompany) && !string.IsNullOrEmpty(hh.FHeadship) && !string.IsNullOrEmpty(hh.FName);
            return t;
        }
        public static bool Validate(this TReward re)
        {
            bool t = !string.IsNullOrEmpty(re.Name) && !string.IsNullOrEmpty(re.ApprovedOrgan) && re.ValidDate != null;
            return t;
        }
        public static bool Validate(this TEmployee emp)
        {
            bool t = string.IsNullOrEmpty(emp.FBornCity) || string.IsNullOrEmpty(emp.FMobileTel) || string.IsNullOrEmpty(emp.FAdress) || !emp.THouseHolds.Any();
            return !t;
        }
        //今后使用DropDownListHelper中的方法
        public static SelectList GetRelationList(this THouseHold hh)
        {
            hrdbEntities ent = new hrdbEntities();
            return new SelectList(ent.TDictRelations, "FRelationCode", "FName", hh.FRelationCode);
        }
        public static SelectList GetGenderList(this THouseHold hh)
        {
            hrdbEntities ent = new hrdbEntities();
            return new SelectList(ent.TDictGenders, "FGenderCode", "FName", hh.FGenderCode);
            
        }
        public static SelectList GetPartyList(this THouseHold hh)
        {
            hrdbEntities ent = new hrdbEntities();
            return new SelectList(ent.TDictParties, "FPartyCode", "FName", hh.FPartyCode);
            
        }
    }
}